Page({
  data: {
    currentRole: 'customer'
  },

  onLoad: function() {
    // 获取当前用户角色
    const app = getApp();
    this.setData({
      currentRole: app.globalData.role
    });
  },

  switchToCustomer: function() {
    this.setData({
      currentRole: 'customer'
    });
  },

  switchToTechnician: function() {
    this.setData({
      currentRole: 'technician'
    });
  },

  switchToAdmin: function() {
    this.setData({
      currentRole: 'admin'
    });
  },

  confirmRole: function() {
    const app = getApp();
    const newRole = this.data.currentRole;
    
    // 如果角色没有变化，直接返回
    if (newRole === app.globalData.role) {
      wx.switchTab({
        url: this.getTabUrl(newRole)
      });
      return;
    }
    
    // 调用后端切换角色接口
    app.request({
      url: `${app.globalData.baseUrl}/user/switch_role/`,
      method: 'POST',
      data: {
        role: newRole
      },
      success: (res) => {
        if (res.statusCode === 200) {
          // 更新全局角色
          app.globalData.role = newRole;
          
          // 跳转到对应角色的首页
          wx.switchTab({
            url: this.getTabUrl(newRole)
          });
        } else {
          wx.showToast({
            title: '切换失败',
            icon: 'none'
          });
        }
      }
    });
  },

  getTabUrl: function(role) {
    switch(role) {
      case 'customer':
        return '/pages/customer/index';
      case 'technician':
        return '/pages/technician/index';
      case 'admin':
        return '/pages/admin/index';
      default:
        return '/pages/customer/index';
    }
  }
})